$(function () {
    var sylwh = {
        grid: $("#grid_sylwh"),
        gridOptions: {
            url: $.webPath() + "/fkyj/yjbb/sylwhList",
            toolbar: '#toolbar_sylwh',
            fitColumns: true,
            fit: true,
            rownumbers: true,
            pageNumber: 1,
            pagination: true,
            singleSelect: true,
            columns: [
                [
                    {field: 'id', title: 'Id', hidden: true},
                    {field: 'qc', title: '全称', width: 150},
                    {field: 'jc', title: "简称", width: 150},
                    {field: 'bm', title: '编码', width: 80},
                    {field: 'jzsyl', title: '预警值(%)', editor: {type: 'text'}, width: 50},
                    {field: 'createdDate', title: '创建日期', width: 80},
                    {field: 'updatedDate', title: '修改日期', width: 80},
                    {field: 'cz', title: '操作', formatter: function (value, row, index) {
                        if (value && value == 'save')
                            return '<a index="' + index + '" name="save" href="javascript:void(0)" onclick="save_sylwh(' + index + ')"></a>'
                        return '<a index="' + index + '" name="edit" href="javascript:void(0)" onclick="edit_sylwh(' + index + ')"></a>'
                    }, width: 30, align: 'center'}
                ]
            ],
            onLoadSuccess: function () {
                $("a[name='edit']").linkbutton({
                    plain: true,
                    iconCls: 'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain: true,
                    iconCls: 'icon-save'
                });
            },
            onBeforeEdit: function (rowIndex, rowData) {
                sylwh.grid.edatagrid("updateRow", {
                    index: rowIndex,
                    row: $.extend({}, rowData, {cz: 'save'})

                });
                $("a[name='save']").linkbutton({
                    plain: true,
                    iconCls: 'icon-save'
                });
            },
            onAfterEdit: function (index, row) {
                var url = $.webPath() + "/fkyj/yjbb/save_sylwh";
                $.post(url, row, function (data) {
                    if (data.statusCode == 200) {
                        sylwh.grid.datagrid('updateRow', {
                            index: index,
                            row: $.extend({}, row, {cz: 'edit', 'id': data.id, 'jzsyl': data.jzsyl, 'qc': data.qc})
                        });
                        $("a[name='edit']").linkbutton({
                            plain: true,
                            iconCls: 'icon-edit'
                        });
                    } else {
                        $.messager.alert('提示消息', data.message);
                        sylwh.grid.edatagrid("beginEdit", index);
                    }
                }, 'json');
            }
        }
    }
    sylwh.grid.edatagrid(sylwh.gridOptions);

    //查询按钮
    $("#btn_sylwh_search").click(function () {
        sylwh_search();
    });

    //批量导入
    $("#btn_sylwh_pldr").click(function () {
        var win = $.window({
            title: "客户基准损益率批量导入",
            width: 550,
            height: 340,
            cache: false,
            url: $.webPath() + "/fkyj/sylwh/sylwh_upload",
            modal: true,
            buttons: [
                {
                    text: '确认',
                    handler: function () {
                        $('#sylwh_uploadSubject').form('submit', {
                            onSubmit: function () {
                                $(win).mask();
                            },
                            success: function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#grid_sylwh').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: json.message,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });

});

// 查询客户损益率
function sylwh_search() {
    $('#grid_sylwh').datagrid('load',
        {
            qcName: $('#sylwh_qc').val(),
            jcName: $('#sylwh_jc').val()
        }
    );
}

function save_sylwh(index) {
    $("#grid_sylwh").edatagrid("endEdit", index);
}
function edit_sylwh(index) {
    $("#grid_sylwh").edatagrid("beginEdit", index);
}